extends Node2D

@onready var arrow :Node2D = $BesselArrow
@onready var card_container =  $HandCardContainer
@onready var discard_poll =  $DiscardPoll
@onready var draw_poll = $DrawPool
@onready var avail_point_lab = $NextRoundMarginContainer/HBoxContainer/AvailPointLabel
@onready var hand_point_lab = $NextRoundMarginContainer/HBoxContainer/HandPointLabel
@onready var card_select = $CardSelect
var round_data:RoundData = CardManager.round_data

#用于保存临时需要选牌得牌,用于点击取消按钮时返回给手牌
var temp_select_card: Card

func _ready():	
	card_select.table_hand_card_container = $HandCardContainer
	Events.card_aim_started.connect(_on_card_aim_started)
	Events.card_aim_ended.connect(_on_card_aim_ended)
	Events.card_table_point_update.connect(_on_card_table_point_update)
	
	
func prepare():
	CardManager.start_battle()
	discard_poll.poll = CardManager.discard_pool
	draw_poll.poll = CardManager.draw_pool
	var hand_cards_size = CardManager.hand_cards.size()
	for i in range(0, hand_cards_size):
		card_container.add(init_card_view(CardManager.hand_cards[i]))
	avail_point_lab.set_text(str(CardManager.avail_point))
	hand_point_lab.set_text(str(CardManager.hand_point))
	

func _process(_delta):
	if arrow.startPos != null:
		arrow.reset(get_viewport().get_mouse_position())

	
func draw_card_no_shuffle():
	if CardManager.draw_pool.get_card_count() < 1:
		printerr("不应该出现的情况")
		return
	var card = CardManager.draw_card_no_shuffle()
	var card_view = init_card_view(card)
	TestGlobals.new_draw_card = card_view
	draw_poll_draw_tween(card_view)
	

func _on_card_table_point_update():
	avail_point_lab.set_text(str(CardManager.avail_point))
	hand_point_lab.set_text(str(CardManager.hand_point))
	

func _on_card_aim_started(card_view: CardView) -> void:
	if not card_view.should_show_arrow():
		return
	arrow.startPos =  card_view.global_position
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	

func _on_card_aim_ended(_card: CardView) -> void:
	arrow.startPos = null
	arrow.hide()
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)



func init_card_view(card:Card) -> CardView:
	var card_view:CardView = card.card_scene.instantiate()
	card_view.card = card
	card_view.can_use = false
	return card_view
	
		
func draw_poll_draw_tween(card_view:CardView):
	card_view.position = Vector2(0,1080)
	add_child(card_view)
	var tween:Tween = card_view.create_tween()
	var target_position = Vector2(get_viewport_rect().size.x/2, get_viewport_rect().size.y/2)
	tween.tween_property(card_view, "position", target_position, 0.6).set_trans(Tween.TRANS_QUART).set_ease(Tween.EASE_OUT)
	tween.play()
	await tween.finished
	card_view.can_use = true
	card_view.reparent(card_container)
	card_view.card_container = card_container
	card_container.do_organize_cards()
	
	
func _on_next_round_pressed():
	Events.next_round.emit()


func pre_drad_card(card_number):
	var card_count = CardManager.draw_pool.get_card_count() + CardManager.discard_pool.get_card_count() 
	if card_count <= 0:
		return 0
	if CardManager.draw_pool.get_card_count() < card_number:
		#洗牌+抽牌
		return 0.5 + card_number * 0.3
	else:
		return card_number * 0.3

func drad_card(card_number = CardManager.pre_draw_card + CardManager.round_data.next_draw_card):
	if CardManager.draw_pool.get_card_count() < 0:
		printerr("不应该出现的情况")
		return
		
	var card_count = CardManager.draw_pool.get_card_count() + CardManager.discard_pool.get_card_count() 
	if card_count <= 0:
		print("牌堆为空")
		return
	
	if CardManager.discard_pool.get_card_count() != 0:
		if CardManager.draw_pool.get_card_count() < card_number:
			shuffle_and_drad_card(card_number)
			return
				
	var tween = create_tween()
	tween.set_loops(min(card_number,card_count))
	tween.tween_callback(func():
		var card = CardManager.draw_card()
		var card_view = init_card_view(card)
		draw_poll_draw_tween(card_view)
	)
	tween.tween_interval(0.3)
	await tween.finished
	

func shuffle_and_drad_card(card_number = CardManager.pre_draw_card):
	var tween = create_tween();
	tween.tween_callback(
		func ():
			draw_poll.shuffle.emit()
			discard_poll.shuffle.emit()
	)
	tween.tween_interval(0.5)
	tween.finished.connect(
		func():
			drad_card(card_number)
	)
	

func recovery_player_card():
	card_container.recovery_player_card()

func on_battle_win():
	var tween = create_tween()
	var temp_gp = card_container.get_global_position()
	tween.tween_property(card_container, "global_position", temp_gp + Vector2(0,500), 0.3)
	tween.tween_interval(0.35)
	tween.finished.connect(
		func():
			get_tree().paused = true
	)

func show_next_round_button(next_round_show):
	$NextRoundMarginContainer/HBoxContainer/NextRound.visible = next_round_show
	if next_round_show:
		card_container.can_use_card()
	else:
		card_container.cant_use_card()
	
	
	
